﻿using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace DreamCube.Foundation.Basic.Utility
{
    public static class MySecurity
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="inStr"></param>
        /// <returns></returns>
        public static String EncryptMD5(String text)
        {
            if (String.IsNullOrEmpty(text)) return "";
            String outputStr = String.Empty;
            Byte[] dataToHash = (new System.Text.ASCIIEncoding()).GetBytes(text);
            Byte[] hashvalue = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(dataToHash);
            return Encoding.UTF8.GetString(hashvalue);
        }

        /// <summary>
        /// 使用dll中默认的key值对数据进行加密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
#if NET20
        public static String Encrypt(String text)
#else
        public static String Encrypt(this String text)
#endif
        {
            return EncryptByKey(text, Properties.Resources.CryptKey);
        }

        /// <summary>
        /// 使用dll中默认的key值对数据进行解密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
#if NET20
        public static String Decrypt(String text)
#else
        public static String Decrypt(this String text)
#endif
        {
            return DecryptByKey(text, Properties.Resources.CryptKey);
        }

        /// <summary>
        /// 根据提供的Key值，对数据进行加密（可逆的加密方法）
        /// </summary>
        /// <param name="text">明文</param>
        /// <param name="key">密匙</param>
        /// <returns>密文</returns>
#if NET20
        public static String EncryptByKey(String text, String key)
#else
        public static String EncryptByKey(this String text, String key)
#endif
        {
            if (String.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            Byte[] inputByteArray = Encoding.Default.GetBytes(text);
            //Key和IV的Byte数组长度为8即可
            Byte[] inputKey = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 8));
            des.Key = inputKey;
            des.IV = inputKey; 
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }

        /// <summary>
        /// 根据提供的key，把密文解密
        /// </summary>
        /// <param name="text">密文</param>
        /// <param name="key">key</param>
        /// <returns>明文</returns>
#if NET20
        public static String DecryptByKey(String text, String key)
#else
        public static String DecryptByKey(this String text, String key)
#endif
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            Byte[] inputByteArray = Convert.FromBase64String(text);
            //Key和IV的Byte数组长度为8即可
            Byte[] inputKey = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 8));
            des.Key = inputKey;
            des.IV = inputKey;
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }
    }
}
